package com.IntelligentCommunity.deploy.service.impl;

import com.IntelligentCommunity.common.core.utils.StringUtils;
import com.IntelligentCommunity.deploy.cache.TaskFaceCache;
import com.IntelligentCommunity.deploy.domain.DeployAlarm;
import com.IntelligentCommunity.deploy.domain.DeployTaskFace;
import com.IntelligentCommunity.deploy.domain.vo.AlarmFace;
import com.IntelligentCommunity.deploy.domain.vo.DeployTaskFaceVO;
import com.IntelligentCommunity.deploy.mapper.DeployAlarmMapper;
import com.IntelligentCommunity.deploy.run.CacheJob;
import com.IntelligentCommunity.deploy.service.IDeployAlarmService;
import com.IntelligentCommunity.deploy.service.IDeployTaskFaceService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 抓拍记录Service业务层处理
 *
 * @author kwzjx
 * @date 2024-07-23
 */
@Service
@Log4j2
public class DeployAlarmServiceImpl extends ServiceImpl<DeployAlarmMapper, DeployAlarm> implements IDeployAlarmService {
    @Autowired
    private DeployAlarmMapper deployAlarmMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IDeployTaskFaceService deployTaskFaceService;

    @Autowired
    private CacheJob cacheJob;

    /**
     * 查询抓拍记录
     *
     * @param id 抓拍记录主键
     * @return 抓拍记录
     */
    @Override
    public DeployAlarm selectDeployAlarmById(Long id)
    {
        return deployAlarmMapper.selectDeployAlarmById(id);
    }

    /**
     * 查询抓拍记录列表
     *
     * @param deployAlarm 抓拍记录
     * @return 抓拍记录
     */
    @Override
    public List<DeployAlarm> selectDeployAlarmList(DeployAlarm deployAlarm)
    {
        return deployAlarmMapper.selectDeployAlarmList(deployAlarm);
    }

    /**
     * 新增抓拍记录
     *
     * @param deployAlarm 抓拍记录
     * @return 结果
     */
    @Override
    public int insertDeployAlarm(DeployAlarm deployAlarm)
    {
        return deployAlarmMapper.insertDeployAlarm(deployAlarm);
    }

    /**
     * 修改抓拍记录
     *
     * @param deployAlarm 抓拍记录
     * @return 结果
     */
    @Override
    public int updateDeployAlarm(DeployAlarm deployAlarm)
    {
        return deployAlarmMapper.updateDeployAlarm(deployAlarm);
    }

    /**
     * 批量删除抓拍记录
     *
     * @param ids 需要删除的抓拍记录主键
     * @return 结果
     */
    @Override
    public int deleteDeployAlarmByIds(Long[] ids)
    {
        return deployAlarmMapper.deleteDeployAlarmByIds(ids);
    }

    /**
     * 删除抓拍记录信息
     *
     * @param id 抓拍记录主键
     * @return 结果
     */
    @Override
    public int deleteDeployAlarmById(Long id)
    {
        return deployAlarmMapper.deleteDeployAlarmById(id);
    }

    @Override
    public void alarm2mq(DeployAlarm deployAlarm) {

        if (StringUtils.isBlank(deployAlarm.getSubject())){
            throw new RuntimeException("任务主题不能为空");
        }

        ConcurrentHashMap<String, DeployTaskFaceVO> taskFaceCacheMap = TaskFaceCache.getInstance().getTaskFaceCacheMap();// 获取缓存1
        if (taskFaceCacheMap.isEmpty()){
            cacheJob.run();
        }
        taskFaceCacheMap =TaskFaceCache.getInstance().getTaskFaceCacheMap();

        String subject = deployAlarm.getSubject().split(",")[1];
        // 获取缓存
        taskFaceCacheMap.forEach((k, v) -> {
                if (Integer.valueOf(subject).equals(Integer.valueOf(v.getId().toString())) &&
                        v.getStatus() == 1 &&
                        v.getSimilarity() < deployAlarm.getSimilarity() &&
                        v.getStartTime().getTime() <= deployAlarm.getCaptureTime().getTime() &&
                        v.getEndTime().getTime() >= deployAlarm.getCaptureTime().getTime() ) {
                        // 发送消息
                        AlarmFace alarmFace = new AlarmFace(deployAlarm, v);
                        rabbitTemplate.convertAndSend("fanoutExchange", "", JSON.toJSONString(alarmFace));
                        log.info("生产成功");
                    }
            });
    }

    @Override
    public void alarmAdd(DeployAlarm deployAlarm) {
        deployAlarm.setCaptureTime(new Date());
        save(deployAlarm);
    }
}
